package com.example.onlinemonitoring.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.onlinemonitoring.model.Psdasc;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.service.PsdascService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Api(value = "主动干预消弧控制器一次系统图接口", tags = { "主动干预消弧控制器一次系统图接口" })
@RestController
@RequestMapping("/primaryCircuitrySystem")
public class PsdascController {

    @Autowired
    private PsdascService psdascService;

    @ApiOperation(value="新增", notes= "新增")
    @PostMapping("/createPsdasc")
    public MyResult<Integer> addPsdasc(@RequestBody String jsonParam,
                                       @RequestParam(value = "channelnum", required = true) Integer channelnum){
        JSONObject jsonObject = JSONObject.parseObject(jsonParam);
        Psdasc psdasc = jsonObject.toJavaObject(jsonObject, Psdasc.class);
        psdasc.setChannelnum(channelnum);
        MyResult<Integer> m = new MyResult<Integer>();
        if(psdascService.insert(psdasc)>=1){
            m.setMsg("修改成功");
            m.setCode(0);
        }else{
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return  m;
    }

    @ApiOperation(value="更新", notes= "更新")
    @PostMapping("/updatePsdasc")
    public MyResult<Integer> updatePsdasc(@RequestBody String jsonParam,
                                          @RequestParam(value = "channelnum", required = true) Integer channelnum){
        JSONObject jsonObject = JSONObject.parseObject(jsonParam);
        Psdasc psdasc = jsonObject.toJavaObject(jsonObject, Psdasc.class);
        psdasc.setStationid(channelnum);
        Example example = new Example(Psdasc.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("channelnum",channelnum);
        MyResult<Integer> m = new MyResult<Integer>();
        if(psdascService.updateByExample(psdasc,example)>=1){
            m.setMsg("修改成功");
            m.setCode(0);
        }else{
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return  m;
    }

    @ApiOperation(value="查询", notes= "查询")
    @GetMapping("/selectPsdasc")
    public MyResult<Psdasc> selectPsdasc(@RequestParam(value = "channelnum", required = true) Integer channelnum){
        Example example = new Example(Psdasc.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("channelnum",channelnum);
        MyResult<Psdasc> result = new MyResult<Psdasc>();
        List<Psdasc> psdascs = psdascService.selectByExample(example);
        if (psdascs.size() != 0) {
            result.setCode(0);
            result.setMsg("success");
            result.setCount(psdascs.size());
            result.setData(psdascs);
        } else {
            result.setCode(1);
            result.setMsg("数据为空");
        }
        return result;
    }
}
